package com.wwwarehouse.carddesk.fragment.teamfragment.checkdownclassfragemnt;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.teambean.RecyclePowerBean;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoke.ni on 2017/10/12.
 */
public class TeamPRUncollectedDetailFragment extends BaseFragment {

    private CustomViewPagerInternal mCustomViewPagerInternal;
    private List<Fragment> mFragmentList;
    private MyFragmentPagerAdapter mViewPagerAdapter;
    private CirclePageIndicator mCirclePagerIndicator;
    private int mFragmentListSize;

    private StateLayout                        mStateLayout;
    private List<RecyclePowerBean.DetailsBean> list;

    public static final int PAGE_SIZE = 10;
    private SlideBarView mSlideBarView;
    private View mView;

    public static TeamPRUncollectedDetailFragment newInstance(List<RecyclePowerBean.DetailsBean> list) {
        TeamPRUncollectedDetailFragment fragment = new TeamPRUncollectedDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("details", (ArrayList<? extends Parcelable>) list);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            list = bundle.getParcelableArrayList("details");
        }
        mFragmentList = new ArrayList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_page_detail_distribute_authority_team, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        // findViewById
        mSlideBarView = findView(mView,R.id.sbv_slide);
        mCustomViewPagerInternal = $(R.id.cvpi);
        mCirclePagerIndicator = $(R.id.cpi);
        mStateLayout = $(R.id.slt);
        mStateLayout.showProgressView(true);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof TeamPRUncollectedDetailFragment) {
            mActivity.setTitle(getString(R.string.teamdesk_user_pr_unreceive_permission));
        }
    }

    @Override
    public void requestDatas() {
        mStateLayout.showContentView();
        if (list == null)
            return;
        int total = list.size();
        int remainder = total % PAGE_SIZE;
        if (remainder == 0) {
            mFragmentListSize = total / PAGE_SIZE;
        } else {
            mFragmentListSize = total / PAGE_SIZE + 1;
        }
        if (mFragmentListSize > 1) {
            for (int i = 0; i < mFragmentListSize; i++) {
                List<RecyclePowerBean.DetailsBean> mDataList = new ArrayList<>();
                for (int j = 0; j < PAGE_SIZE; j++) {
                    if (i * PAGE_SIZE + j <list.size())
                        mDataList.add(list.get(i * PAGE_SIZE + j));
                }
                mFragmentList.add(TeamPRUncollectedDetailListFragment.newInstance(i + 1 + "", mDataList));
            }
        } else {
            mFragmentList.add(TeamPRUncollectedDetailListFragment.newInstance(1 + "", list));
        }
        mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList);
        mCustomViewPagerInternal.setAdapter(mViewPagerAdapter);
        mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal);
        if (mFragmentListSize <= 6) {
            mCirclePagerIndicator.setVisibility(View.VISIBLE);
            mSlideBarView.setVisibility(View.GONE);
            mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal, 0);
        } else {
            mCirclePagerIndicator.setVisibility(View.GONE);
            mSlideBarView.setVisibility(View.VISIBLE);
            mSlideBarView.setTotalPage(mFragmentListSize);
            mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                @Override
                public void onSlideChange(int i) {
                    mCustomViewPagerInternal.setCurrentItem(mFragmentListSize, false);
                }
            });
            mSlideBarView.setViewPagerInternal(mCustomViewPagerInternal);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity) mActivity).hideBackBt();
    }


}
